package com.hunttown.mes.impl;

import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.rpc.api.AnalysisManageRpcService;
import com.hunttown.mes.rpc.domain.AnalysisManageDTO;
import com.hunttown.mes.rpc.domain.query.AnalysisManageDTOQuery;
import com.hunttown.mes.service.AnalysisManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.hunttown.mes.convert.AnalysisManageConverts.convert;

/**
 * created by wangjunfu on 2018-11-26 14:25:43
 */
@Service
public class AnalysisManageRpcServiceImpl implements AnalysisManageRpcService {

    private final Logger logger = LoggerFactory.getLogger(AnalysisManageRpcServiceImpl.class);

    private final AnalysisManageService analysisManageService;

    @Autowired
    public AnalysisManageRpcServiceImpl(AnalysisManageService analysisManageService) {
        this.analysisManageService = analysisManageService;
    }

    @Override
    public AnalysisManageDTO insert(AnalysisManageDTO analysisManageDTO) {
        try {
            return convert(analysisManageService.insert(convert(analysisManageDTO)));
        } catch (Exception e) {
            logger.error("AnalysisManage create failed.", e);
            throw new RuntimeException("AnalysisManage create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(AnalysisManageDTO analysisManageDTO) {
        try {
            return analysisManageService.updateInfoById(convert(analysisManageDTO));
        } catch (Exception e) {
            logger.error("AnalysisManage update by id failed.", e);
            throw new RuntimeException("AnalysisManage update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(AnalysisManageDTOQuery analysisManageDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisManageDTOQuery.getW_id() != null && analysisManageDTOQuery.getW_id() > 0) {
                query.put("w_id", analysisManageDTOQuery.getW_id());
            }

            if (StringUtils.isNotBlank(analysisManageDTOQuery.getW_mobile())) {
                query.put("w_mobile", analysisManageDTOQuery.getW_mobile());
            }

            if (analysisManageDTOQuery.getEntId() != null && analysisManageDTOQuery.getEntId() > 0) {
                query.put("entId", analysisManageDTOQuery.getEntId());
            }

            if (analysisManageDTOQuery.getLastLoginTime() != null) {
                query.put("lastLoginTime", analysisManageDTOQuery.getLastLoginTime());
            }

            if (analysisManageDTOQuery.getLastLoginIp() != null && !analysisManageDTOQuery.getLastLoginIp().equals("")) {
                query.put("lastLoginIp", analysisManageDTOQuery.getLastLoginIp());
            }

            if (analysisManageDTOQuery.getLoginTimes() != null && analysisManageDTOQuery.getLoginTimes() > 0) {
                query.put("loginTimes", analysisManageDTOQuery.getLoginTimes());
            }

            if (analysisManageDTOQuery.getIds() != null && !analysisManageDTOQuery.getIds().equals("")) {
                query.put("ids", analysisManageDTOQuery.getIds());
            }

            if (analysisManageDTOQuery.getIsBindWxapp() != null) {
                query.put("isBindWxapp", analysisManageDTOQuery.getIsBindWxapp());
            }

            if (analysisManageDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", analysisManageDTOQuery.getStopFlag());
            }

            if (analysisManageDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisManageDTOQuery.getDeleteFlag());
            }

            return analysisManageService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("AnalysisManage update by query failed.", e);
            throw new RuntimeException("AnalysisManage update by query failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageDTO selectById(Integer id) {
        try {
            return convert(analysisManageService.selectById(id));
        } catch (Exception e) {
            logger.error("AnalysisManage select by id failed.", e);
            throw new RuntimeException("AnalysisManage select by id failed, Please order your code.", e);
        }
    }

    @Override
    public AnalysisManageDTO selectByMobileForLogin(String mobile) {
        return convert(analysisManageService.selectByMobileForLogin(mobile));
    }

    @Override
    public AnalysisManageDTO selectObjByQuery(AnalysisManageDTOQuery analysisManageDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (analysisManageDTOQuery.getId() != null) {
                query.put("id", analysisManageDTOQuery.getId());
            }

            if (analysisManageDTOQuery.getEntId() != null) {
                query.put("entId", analysisManageDTOQuery.getEntId());
            }

            if (analysisManageDTOQuery.getAdminName() != null && !analysisManageDTOQuery.getAdminName().equals("")) {
                query.put("adminName", analysisManageDTOQuery.getAdminName());
            }

            if (analysisManageDTOQuery.getNickName() != null && !analysisManageDTOQuery.getNickName().equals("")) {
                query.put("nickName", analysisManageDTOQuery.getNickName());
            }

            if (analysisManageDTOQuery.getMobile() != null && !analysisManageDTOQuery.getMobile().equals("")) {
                query.put("mobile", analysisManageDTOQuery.getMobile());
            }

            if (analysisManageDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", analysisManageDTOQuery.getStopFlag());
            }

            if (analysisManageDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", analysisManageDTOQuery.getDeleteFlag());
            }

            return convert(analysisManageService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("AnalysisManage select obj by query failed.", e);
            throw new RuntimeException("AnalysisManage select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<AnalysisManageDTO> getListForPage(AnalysisManageDTOQuery analysisManageDTOQuery) {
        Query query = Query.page(analysisManageDTOQuery.getPn(), analysisManageDTOQuery.getPageSize());

        if (analysisManageDTOQuery.getId() != null && analysisManageDTOQuery.getId() > 0) {
            query.put("id", analysisManageDTOQuery.getId());
        }

        if (analysisManageDTOQuery.getIds() != null) {
            query.put("ids", analysisManageDTOQuery.getIds());
        }

        if (analysisManageDTOQuery.getOnJob() != null && analysisManageDTOQuery.getOnJob() == 1) {
            query.put("stopFlag", 0);
        }

        if (analysisManageDTOQuery.getAreaId() != null && analysisManageDTOQuery.getAreaId() != -9999) {
            query.put("areaId", analysisManageDTOQuery.getAreaId());
        }

        if (analysisManageDTOQuery.getAreaIds() != null && analysisManageDTOQuery.getAreaIds() != "") {
            query.put("areaIds", analysisManageDTOQuery.getAreaIds());
        }

        if (analysisManageDTOQuery.getAfterId() != null) {
            query.put("afterId", analysisManageDTOQuery.getAfterId());
        }

        if (analysisManageDTOQuery.getDepIds() != null) {
            query.put("depIds", analysisManageDTOQuery.getDepIds());
        }

        if (analysisManageDTOQuery.getGradeId() != null) {
            query.put("gradeId", analysisManageDTOQuery.getGradeId());
        }

        if (StringUtils.isNotBlank(analysisManageDTOQuery.getLikeName())) {
            query.put("likeName", analysisManageDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(analysisManageDTOQuery.getLikeNickName())) {
            query.put("likeNickName", analysisManageDTOQuery.getLikeNickName());
        }

        if (StringUtils.isNotBlank(analysisManageDTOQuery.getLikeMobile())) {
            query.put("likeMobile", analysisManageDTOQuery.getLikeMobile());
        }

        if (StringUtils.isNotBlank(analysisManageDTOQuery.getAdminName())) {
            query.put("adminName", analysisManageDTOQuery.getAdminName());
        }

        if (analysisManageDTOQuery.getIsSuper() != null && analysisManageDTOQuery.getIsSuper() != -9999) {
            query.put("isSuper", analysisManageDTOQuery.getIsSuper());
        }

        if (analysisManageDTOQuery.getIsCharge() != null) {
            query.put("isCharge", analysisManageDTOQuery.getIsCharge());
        }

        if (analysisManageDTOQuery.getIsGroup() != null) {
            query.put("isGroup", analysisManageDTOQuery.getIsGroup());
        }

        if (StringUtils.isNotBlank(analysisManageDTOQuery.getOrderBy())) {
            query.put("orderBy", analysisManageDTOQuery.getOrderBy());
        }

        if (analysisManageDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", analysisManageDTOQuery.getStopFlag());
        }

        if (analysisManageDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", analysisManageDTOQuery.getDeleteFlag());
        }

        List<AnalysisManageDTO> list = convert(analysisManageService.selectByQuery(query));
        Page<AnalysisManageDTO> page = Page.from(analysisManageDTOQuery.getPn(), analysisManageDTOQuery.getPageSize(), list);
        page.setTotalItems(analysisManageService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return analysisManageService.deleteById(id);
        } catch (Exception e) {
            logger.error("AnalysisManage delete failed.", e);
            throw new RuntimeException("AnalysisManage delete failed, Please order your code.", e);
        }
    }

    @Override
    public List<Map<String, String>> selectSimilarManage(String similar) {
        try {
            Query query = Query.page(1, 20);

            if (StringUtils.isNotBlank(similar)) {
                query.put("likeName", similar.trim());
            }

            return analysisManageService.selectSimilarManage(query);

        } catch (Exception e) {
            logger.error("AnalysisManage select similar user failed.", e);
            throw new RuntimeException("AnalysisManage similar user failed, Please order your code.", e);
        }
    }

    @Override
    public Map<String, String> selectManagerRoleMenu(Integer adminId) {
        try {
            if (adminId == null || adminId <= 0) {
                return new HashMap<>();
            }

            Query query = Query.noPage();
            query.put("adminId", adminId);

            return analysisManageService.selectManagerRoleMenu(query);

        } catch (Exception e) {
            logger.error("AnalysisManage select manage role menu failed.", e);
            throw new RuntimeException("AnalysisManage manage role menu failed, Please order your code.", e);
        }
    }

    @Override
    public Long countByQuery(AnalysisManageDTOQuery analysisManageDTOQuery) {
        Query query = Query.page(analysisManageDTOQuery.getPn(), analysisManageDTOQuery.getPageSize());

        if (analysisManageDTOQuery.getId() != null && analysisManageDTOQuery.getId() > 0) {
            query.put("id", analysisManageDTOQuery.getId());
        }

        if (analysisManageDTOQuery.getIds() != null) {
            query.put("ids", analysisManageDTOQuery.getIds());
        }

        if (analysisManageDTOQuery.getAreaId() != null && analysisManageDTOQuery.getAreaId() != -9999) {
            query.put("areaId", analysisManageDTOQuery.getAreaId());
        }

        if (analysisManageDTOQuery.getAreaIds() != null && analysisManageDTOQuery.getAreaIds() != "") {
            query.put("areaIds", analysisManageDTOQuery.getAreaIds());
        }

        if (analysisManageDTOQuery.getAfterId() != null) {
            query.put("afterId", analysisManageDTOQuery.getAfterId());
        }

        if (analysisManageDTOQuery.getDepIds() != null) {
            query.put("depIds", analysisManageDTOQuery.getDepIds());
        }

        if (analysisManageDTOQuery.getGradeId() != null) {
            query.put("gradeId", analysisManageDTOQuery.getGradeId());
        }

        if (StringUtils.isNotBlank(analysisManageDTOQuery.getLikeName())) {
            query.put("likeName", analysisManageDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(analysisManageDTOQuery.getLikeNickName())) {
            query.put("likeNickName", analysisManageDTOQuery.getLikeNickName());
        }

        if (StringUtils.isNotBlank(analysisManageDTOQuery.getLikeMobile())) {
            query.put("likeMobile", analysisManageDTOQuery.getLikeMobile());
        }

        if (StringUtils.isNotBlank(analysisManageDTOQuery.getAdminName())) {
            query.put("adminName", analysisManageDTOQuery.getAdminName());
        }

        if (analysisManageDTOQuery.getIsSuper() != null && analysisManageDTOQuery.getIsSuper() != -9999) {
            query.put("isSuper", analysisManageDTOQuery.getIsSuper());
        }

        if (analysisManageDTOQuery.getIsCharge() != null) {
            query.put("isCharge", analysisManageDTOQuery.getIsCharge());
        }

        if (analysisManageDTOQuery.getIsGroup() != null) {
            query.put("isGroup", analysisManageDTOQuery.getIsGroup());
        }

        if (StringUtils.isNotBlank(analysisManageDTOQuery.getOrderBy())) {
            query.put("orderBy", analysisManageDTOQuery.getOrderBy());
        }

        if (analysisManageDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", analysisManageDTOQuery.getStopFlag());
        }

        if (analysisManageDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", analysisManageDTOQuery.getDeleteFlag());
        }

        return analysisManageService.countByQuery(query);
    }
}